package com.example.piece.ui.activity

/**
 * 设置页面
 */
import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Route
import com.baidu.aip.asrwakeup3.core.util.AuthUtil
import com.baidu.aip.asrwakeup3.core.wakeup.MyWakeup
import com.baidu.aip.asrwakeup3.core.wakeup.WakeUpResult
import com.baidu.aip.asrwakeup3.core.wakeup.listener.SimpleWakeupListener
import com.baidu.speech.asr.SpeechConstant
import com.example.base.anr.AnrMonitor
import com.example.base.anr.DeadLockMonitor
import com.example.base.aop.click.RepeatClick
import com.example.base.aop.permission.PermissionRequest
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.navigationTo
import com.example.base.ui.activity.BaseActivity
import com.example.base.ui.view.broccoli.util.LogUtil
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivitySettingBinding

@Route(path = RoutePath.MAIN_SETTING)
class SettingActivity : Layer3Activity<ActivitySettingBinding, ViewDataBinding>() {
    override val centerLayoutRes: Int = R.layout.activity_setting
    override val title: String
        get() = "设置"

    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = true
    override val leftIcon1Res: Int = R.drawable.ic_back

    var myWakeup: MyWakeup? = null

    override fun onClickLeftIcon1(view: View) {
        onBackPressed()
        mCenterViewBinding?.sw?.setOnCheckedChangeListener { buttonView, isChecked ->
            initVoice()
        }

    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        try {
            myWakeup = MyWakeup(this, object : SimpleWakeupListener() {
                override fun onSuccess(word: String?, result: WakeUpResult?) {
                    super.onSuccess(word, result)
                    EventBusUtil.sendEvent(Event(Key.EventKey.EVENT_DOUYIN_MSG, word))
                }
            })
        } catch (e: Exception) {

        }

    }

    @PermissionRequest(
        value = [
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
        ]
    )
    private fun initVoice() {
        // 改为 SimpleWakeupListener 后，不依赖handler，但将不会在UI界面上显示
        // 基于DEMO唤醒词集成第1.1, 1.2, 1.3步骤

        start()

        LogUtil.logE("dddddddd")

    }


    // 点击“开始识别”按钮
    // 基于DEMO唤醒词集成第2.1, 2.2 发送开始事件开始唤醒
    private fun start() {
        val params = AuthUtil.getParam()
        params[SpeechConstant.WP_WORDS_FILE] = "assets:///WakeUp.bin"
        // "assets:///WakeUp.bin" 表示WakeUp.bin文件定义在assets目录下

        // params.put(SpeechConstant.ACCEPT_AUDIO_DATA,true);
        // params.put(SpeechConstant.IN_FILE,"res:///com/baidu/android/voicedemo/wakeup.pcm");
        // params里 "assets:///WakeUp.bin" 表示WakeUp.bin文件定义在assets目录下
        myWakeup?.start(params)
    }

    // 基于DEMO唤醒词集成第4.1 发送停止事件
    protected fun stop() {
        myWakeup?.stop()
    }

    @RepeatClick
    fun userInfo(view: View) {
        navigationTo(RoutePath.MAIN_USER_INFO_ACTIVITY)

    }

    @RepeatClick
    fun safe(view: View) {//账号与安全
        navigationTo(RoutePath.MAIN_SAFE)

    }

    @RepeatClick
    fun xieyi(view: View) {
        navigationTo(
            RoutePath.MAIN_H5_ACTIVITY, mutableMapOf(
                Key.DataKey.H5_TITLE to "隐私协议",
                Key.DataKey.H5_URL to "https://res.villaday.com/static/pages/1907/19/190717-19.html"
            )
        )
    }
    @RepeatClick
    fun aboutUs(view: View) {
        navigationTo(RoutePath.MAIN_ABOUTUS)

    }


}